import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/utils/sp_utils.dart';
import 'package:flutterlib/common/utils/utils.dart';
import 'package:flutterlib/model/interest/interest_bean.dart';
import 'package:flutterlib/widgets/sortable_gridview.dart';

class Page extends StatefulWidget {


  Page();

  @override
  State createState() {
    return _PageState();
  }
}

class _PageState extends State<Page> {
  List<InterestBean> interests = allInterestsFromSP;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("兴趣排序", style: Theme.of(context).textTheme.headline3),
        backgroundColor: QuhuiColors.miWhite,
        leading: MyBackButton(
            color: Colors.black),

      ),
      body: Padding(
              padding: EdgeInsets.only(top: 5, left: 10, right: 10),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.fromLTRB(0, 8.0, 0, 8.0),
                    child: Text("长按移动进行排序", style: Theme.of(context).textTheme.headline4,),
                  ),
                  Expanded(
                    child: SortableGridView(
                          interests,
                          childAspectRatio: 1, //宽高3比1
                          crossAxisCount: 4, //3列
                          scrollDirection: Axis.vertical, //竖向滑动
                          canAccept: (oldIndex, newIndex) {
                            return true;
                          },
                          acceptList: (list){
                            SpUtils.putObjectList("interest_order", list);
                            return true;
                          },
                          itemBuilder: (context, data) {
                            return InkWell(
                              onTap: (){
                                NavigatorUtils.gotoInterestDetail(context, data.id);
                              },
                              child: Card(
                                color: getRandomWhiteColor(data.id),
                                  child: Padding(
                                    padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
                                    child: Center(
                                      child: Text(data.name, style: QuhuiConstant.middleTextWhite,),
                                    ),
                                  )),
                            );
                          },
                        ),
                  ),
                ],
              ),
            ),
    );
  }


  Color getRandomWhiteColor(int id) {

    var index = id%17;

    return QuhuiColors.colors[index];
  }

  Widget appBar() {
    return SizedBox(
      height: AppBar().preferredSize.height,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.only(top: 8, left: 8),
            child: Container(
              width: AppBar().preferredSize.height - 8,
              height: AppBar().preferredSize.height - 8,
            ),
          ),
          Expanded(
            child: Center(
              child: Padding(
                padding: const EdgeInsets.only(top: 4),
                child: Text(
                  'Flutter UI',
                  style: TextStyle(
                    fontSize: 22,
                    color: QuhuiColors.white,
                    fontWeight: FontWeight.w700,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  void initState() {

  }
}
